'
'   XC Engine. A simple engine to drive the Internet Explorer web browser
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class Button
	Private objHtmlButton
	Private strHtmlButtonUniqueId
	
	Public Sub Class_Initialize()
		Set objHtmlButton = Nothing
	End Sub
	
	Public Sub Class_Terminate()
		Set objHtmlButton = Nothing
	End Sub
	
	Private Function IsButton(ByVal objHtmlElement)
		IsButton = False
		
		If UCase(objHtmlElement.TagName) = "INPUT" Then
			If UCase(objHtmlElement.Type) = "SUBMIT" Or UCase(objHtmlElement.Type) = "BUTTON" Or UCase(objHtmlElement.Type) = "RESET" Or UCase(objHtmlElement.Type) = "IMAGE" Then
				IsButton = True
			End If
		End If
	End Function
	
	Public Sub WrapElement(ByVal objHtmlElement)
		If Not (objHtmlElement Is Nothing) Then
			If IsButton(objHtmlElement) Then
				strHtmlButtonUniqueId = objHtmlElement.UniqueId
			
				Set objHtmlButton = objHtmlElement
				
				Exit Sub
			End If
		End If

		Err.Raise 10001, "Button->WrapElement", "The Html Element couldn't be wrapped"
	End Sub
	
	Public Function GetProperty(ByVal strPropertyName)
		If Exists() Then
			If Internal_GetProperty(objHtmlButton, strPropertyName, varPropertyName) Then
				GetProperty = varPropertyName
			Else
				Err.Raise 10003, "Button->GetProperty", "The Button doesn't support this property"
			End If
		Else
			Err.Raise 10002, "Button->GetProperty", "The Button doesn't exist"
		End If
	End Function
	
	Public Function Element()
		Set Element = Nothing
		
		If Not Exists() Then
			Err.Raise 10002,  "Button->Element", "The Button doesn't exist"
			
			Exit Function
		End If
		
		Set Element = objHtmlButton
	End Function
	
	Public Function Exists()
		On Error Resume Next
		
		Exists = False
		
		If Not IsButton(objHtmlButton) Then Exit Function
		
		strHtmlElementTagName = objHtmlButton.TagName
		
		If Err.Number = 0 Then
			Exists = True
			
			Exit Function
		End If
		
		Err.Clear
		
		On Error Goto 0
	End Function
	
	Public Sub FireEvent(ByVal strEventName)
		If Exists() Then
			objHtmlButton.FireEvent strEventName
		Else
			Err.Raise 10002, "Button->FireEvent", "The Button doesn't exist"
		End If
	End Sub
	
	Public Sub Click()
		If Not Exists() Then
			Err.Raise 10002, "Button->Click", "The Button doesn't exist"
			
			Exit Sub
		End If
		
		If GetProperty("disabled") Then
			Err.Raise 10004, "Button->Click", "The Button is disabled"
			
			Exit Sub
		End If
		
		Call FireEvent("onFocus")
		'Call FireEvent("onClick")
		
		objHtmlButton.Click
	End Sub
End Class